package com.hongyuan.sellfitness.ui.find.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.inputmethod.EditorInfo;

import com.hongyuan.mvvmhabitx.bus.RxBus;
import com.hongyuan.sellfitness.R;
import com.hongyuan.sellfitness.base.CustomActivity;
import com.hongyuan.sellfitness.base.CustomViewModel;
import com.hongyuan.sellfitness.data.rxbus.SearchFindMessage;
import com.hongyuan.sellfitness.databinding.ActivityFindSearchBinding;
import com.hongyuan.sellfitness.ui.find.viewpage_adapter.FindSearchViewPagerAdapter;

import me.tatarka.bindingcollectionadapter2.BR;

public class FindSearchActivity extends CustomActivity<ActivityFindSearchBinding, CustomViewModel> {

    private String searchText;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_find_search;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initView() {
        super.initView();

        binding.mViewPager.setAdapter(new FindSearchViewPagerAdapter(getSupportFragmentManager()));
        binding.layoutMenu.setupWithViewPager(binding.mViewPager);
        binding.mViewPager.setOffscreenPageLimit(3);

        binding.searchText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                searchText = s.toString();
            }
        });

        binding.searchText.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH){
                RxBus.getDefault().post(new SearchFindMessage(searchText));
                return true;
            }
            return false;
        });

        binding.cancelText.setOnClickListener(v -> finish());
    }
}
